home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2005 February / PCpro_2005_02.ISO / files / opensource / jEdit_4.2 / jedit42install.exe / {app} / jedit.jar / bsh / Modifiers.class (.txt) < prev    next >
Encoding:
Java Class File  |  2004-08-29  |  2.4 KB  |  87 lines

  1. package bsh;
  2.  
  3. import java.io.Serializable;
  4. import java.util.Hashtable;
  5.  
  6. public class Modifiers implements Serializable {
  7.    public static final int CLASS = 0;
  8.    public static final int METHOD = 1;
  9.    public static final int FIELD = 2;
  10.    Hashtable modifiers;
  11.  
  12.    public void addModifier(int context, String name) {
  13.       if (this.modifiers == null) {
  14.          this.modifiers = new Hashtable();
  15.       }
  16.  
  17.       Object got = this.modifiers.put(name, Void.TYPE);
  18.       if (got != null) {
  19.          throw new IllegalStateException("Duplicate modifier: " + name);
  20.       } else {
  21.          int count = 0;
  22.          if (this.hasModifier("private")) {
  23.             ++count;
  24.          }
  25.  
  26.          if (this.hasModifier("protected")) {
  27.             ++count;
  28.          }
  29.  
  30.          if (this.hasModifier("public")) {
  31.             ++count;
  32.          }
  33.  
  34.          if (count > 1) {
  35.             throw new IllegalStateException("public/private/protected cannot be used in combination.");
  36.          } else {
  37.             switch (context) {
  38.                case 0:
  39.                   this.validateForClass();
  40.                   break;
  41.                case 1:
  42.                   this.validateForMethod();
  43.                   break;
  44.                case 2:
  45.                   this.validateForField();
  46.             }
  47.  
  48.          }
  49.       }
  50.    }
  51.  
  52.    public boolean hasModifier(String name) {
  53.       if (this.modifiers == null) {
  54.          this.modifiers = new Hashtable();
  55.       }
  56.  
  57.       return this.modifiers.get(name) != null;
  58.    }
  59.  
  60.    private void validateForMethod() {
  61.       this.insureNo("volatile", "Method");
  62.       this.insureNo("transient", "Method");
  63.    }
  64.  
  65.    private void validateForField() {
  66.       this.insureNo("synchronized", "Variable");
  67.       this.insureNo("native", "Variable");
  68.       this.insureNo("abstract", "Variable");
  69.    }
  70.  
  71.    private void validateForClass() {
  72.       this.validateForMethod();
  73.       this.insureNo("native", "Class");
  74.       this.insureNo("synchronized", "Class");
  75.    }
  76.  
  77.    private void insureNo(String modifier, String context) {
  78.       if (this.hasModifier(modifier)) {
  79.          throw new IllegalStateException(context + " cannot be declared '" + modifier + "'");
  80.       }
  81.    }
  82.  
  83.    public String toString() {
  84.       return "Modifiers: " + this.modifiers;
  85.    }
  86. }
  87.